package com.onec.service.manage.controller;

import com.onec.service.api.dto.response.BaseResponse;
import com.onec.service.api.enums.ResultCodeEnum;
import com.onec.service.api.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author onec
 */
@Slf4j
@ControllerAdvice(basePackages = "com.onec.service.manage.controller")
public class ExceptionHandlerAdvice {

    @ExceptionHandler(value = AuthorizationException.class)
    @ResponseBody
    public BaseResponse handleAuthorizationException(AuthorizationException e) {
        log.info("自定义异常：AuthorizationException:{}", e.getMessage());
        return BaseResponse.error(ResultCodeEnum.UNAUTHORIZED);
    }

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public BaseResponse handleException(Exception e) {
        log.info("自定义异常：Exception:{}", e.getMessage());
        e.printStackTrace();
        return BaseResponse.error(ResultCodeEnum.SERVER_ERROR);
    }


    @ExceptionHandler(value = BaseException.class)
    @ResponseBody
    public BaseResponse handleBaseException(BaseException e) {
        log.info("自定义异常：BaseException:{}", e.getMessage());
        return BaseResponse.error(e);
    }

}
